package com.example.apricots.Net;

import com.example.apricots.BuildConfig;
import com.example.apricots.Net.Base.Api;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 单例请求
 */
public class RequestApi {

    private CommonServices request;
    private static RequestApi instance = new RequestApi();

    public static RequestApi getInstance() {
        return instance;
    }

    public CommonServices getRequestAdapter() {
        if (request == null) {
            OkHttpClient.Builder builder = new OkHttpClient
                    .Builder()
                    .readTimeout(30, TimeUnit.SECONDS)
                    .connectTimeout(20, TimeUnit.SECONDS);
            OkHttpClient httpClient = builder.build();
            if (BuildConfig.DEBUG) {
                //该拦截器用于记录应用中的网络请求的信息
                HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
                httpClient = builder.addInterceptor(logging).build();
            }
            Retrofit retofit = new Retrofit.Builder()
                    .baseUrl(Api.BASEURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            request = retofit.create(CommonServices.class);
            return request;
        }else {
            return request;
        }
    }
}
